#include "ProviderExtensionEnd.h"

#include "core/AppSignals.h"

ProviderExtensionEnd::ProviderExtensionEnd()
    : PropertyProvider()
{
}

void ProviderExtensionEnd::initEditor(
    QtTreePropertyBrowser* editor,
    shared_ptr<BaseElement> entity
)
{
    entity_ = dynamic_pointer_cast<uml::ExtensionEnd>(entity);
    propertyToId.clear();
    PropertyProvider::initEditor(editor, entity);

    QStringList aggregationNames;
    aggregationNames.append("none");
    aggregationNames.append("shared");
    aggregationNames.append("composite");
    addProperty("aggregation", "aggregation", static_cast<int>(entity_->aggregation()), aggregationNames);
    addProperty("isComposite", "isComposite", entity_->isComposite());
    addProperty("isDerived", "isDerived", entity_->isDerived());
    addProperty("isDerivedUnion", "isDerivedUnion", entity_->isDerivedUnion());
    addProperty("isID", "isID", entity_->isID());
    addProperty("isLeaf", "isLeaf", entity_->isLeaf());
    addProperty("isOrdered", "isOrdered", entity_->isOrdered());
    addProperty("isReadOnly", "isReadOnly", entity_->isReadOnly());
    addProperty("isStatic", "isStatic", entity_->isStatic());
    addProperty("isUnique", "isUnique", entity_->isUnique());
    addProperty("lower", "lower", entity_->lower());
    addProperty("name", "name", entity_->name());
    addProperty("qualifiedName", "qualifiedName", entity_->qualifiedName());
    addProperty("upper", "upper", entity_->upper());
    QStringList visibilityNames;
    visibilityNames.append("public");
    visibilityNames.append("private");
    visibilityNames.append("protected");
    visibilityNames.append("package");
    addProperty("visibility", "visibility", static_cast<int>(entity_->visibility()), visibilityNames);

}

void ProviderExtensionEnd::valueChanged(QtProperty* property, bool value)
{
    QString id = propertyToId[property];
    if (id == "isComposite") {
        entity_->setIsComposite(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isDerived") {
        entity_->setIsDerived(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isDerivedUnion") {
        entity_->setIsDerivedUnion(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isID") {
        entity_->setIsID(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isLeaf") {
        entity_->setIsLeaf(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isOrdered") {
        entity_->setIsOrdered(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isReadOnly") {
        entity_->setIsReadOnly(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isStatic") {
        entity_->setIsStatic(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isUnique") {
        entity_->setIsUnique(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderExtensionEnd::valueChanged(QtProperty* property, double value)
{
    QString id = propertyToId[property];
    
}

void ProviderExtensionEnd::valueChanged(QtProperty* property, int value)
{
    QString id = propertyToId[property];
    if (id == "aggregation") {
        entity_->setAggregation(static_cast<AggregationKind>(value));
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "lower") {
        entity_->setLower(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "upper") {
        entity_->setUpper(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "visibility") {
        entity_->setVisibility(static_cast<VisibilityKind>(value));
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderExtensionEnd::valueChanged(QtProperty* property, QString value)
{
    QString id = propertyToId[property];
    if (id == "name") {
        entity_->setName(value);
        emit appSignals->onElementRenamed(this, entity_);
    } else if (id == "qualifiedName") {
        entity_->setQualifiedName(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}
